package cn.edu.ujs.coor.common.oss.service;

import cn.edu.ujs.coor.common.exception.CoorException;
import cn.edu.ujs.coor.common.oss.config.OssProperties;
import cn.edu.ujs.coor.model.User;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 云对象存储
 *
 * @author 柳曾雄
 * @date 2018-08-04
 */
@Service
public class OssService {

    @Autowired
    private OSSClient client;

    @Autowired
    private OssProperties properties;

    /**
     * 文件上传
     * @param file
     * @return
     */
    public String put (MultipartFile file, User user) {
        try {
            String filename = user.getId()+ "-" + user.getName() + "-" + file.getOriginalFilename();
            client.putObject(properties.getBucket(), filename, file.getInputStream());
            return filename;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CoorException("文件上传失败");
        }
    }

    /**
     * 读取文件字节流
     * @param name
     * @return
     */
    public ByteArrayOutputStream get (String name) {
        ByteArrayOutputStream baos = null;
        InputStream is = null;
        try {
            // 初始化流
            baos = new ByteArrayOutputStream();
            OSSObject object = client.getObject(properties.getBucket(), name);

            is = object.getObjectContent();
            int len = 0;
            // 字节流读取缓冲区1M
            byte[] buffer = new byte[1024 * 1024];
            // 读取字节缓冲流，写入字节对象输出流
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            is.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CoorException("文件下载失败");
        }
        return baos;
    }

    /**
     * 批量下载
     * @param names
     * @return
     */
    public ByteArrayOutputStream get (List<String> names) {
        ByteArrayOutputStream baos = null;
        ZipOutputStream zipOutputStream = null;
        try {
            baos = new ByteArrayOutputStream();
            zipOutputStream = new ZipOutputStream(baos);
            // 循环读取字节流
            for (String name : names) {
                ByteArrayOutputStream buffer = get(name);
                ZipEntry zipEntry = new ZipEntry(name);
                zipOutputStream.putNextEntry(zipEntry);
                zipOutputStream.write(buffer.toByteArray());
                zipOutputStream.flush();
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            baos.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new CoorException("文件下载失败");
        }
        return baos;
    }
}
